package com.mai4j.maiadmin.misc;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Random;

/**
 * Created by 席有芳 on 2021/4/5.
 * 生成密码的工具
 *
 * @author 席有芳
 */
public class Password {

    /**
     * a-Z0-9
     */
    private static final String ALL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    /**
     * 随机生成指定长度的字符串
     *
     * @param length length
     * @return String
     */
    public static String salt(int length) {
        StringBuilder token = new StringBuilder();
        int max = ALL.length() - 1;
        for (int i = 0; i < length; i++) token.append(ALL.toCharArray()[new Random().nextInt(max)]);
        return token.toString();
    }

    /**
     * 生成密码
     *
     * @param password password
     * @param salt     salt
     * @return password
     */
    public static String password(String password, String salt) {
        return DigestUtils.sha1Hex(salt + DigestUtils.sha1Hex(salt + DigestUtils.sha1Hex(password)));
    }
}
